package cn.com.homeyoda.aimate.aihealth.ui.health_qa.record;

import android.os.Bundle;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTypeListBean;
import cn.com.homeyoda.aimate.aihealth.bean.UserRecordsBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityTestRecordBinding;
import cn.com.homeyoda.aimate.aihealth.ui.health_qa.HealthQaViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.health_qa.result.HealthQaResultActivity;
import cn.com.homeyoda.aimate.aihealth.ui.message.MessageTypeAdapter;

public class TestRecordActivity extends BaseActivity<ActivityTestRecordBinding, HealthQaViewModel> implements View.OnClickListener {

    private TestRecordAdapter testRecordAdapter;

    private MessageTypeAdapter messageTitleAdapter;
    private List<MessageTypeListBean.DataBean> titleBeanList = new ArrayList<>();

    @Override
    public HealthQaViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(HealthQaViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_test_record;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();

        binding.titleBar.ivBack.setOnClickListener(this);
        binding.titleBar.tvTitle.setText(getString(R.string.test_records));

        viewModel.getTypeList(false);

        initRecyclerView();
    }

    private void initRecyclerView() {

        messageTitleAdapter = new MessageTypeAdapter(R.layout.message_title_item, titleBeanList, getApplicationContext());
        binding.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
        binding.setAdapter(messageTitleAdapter);

        messageTitleAdapter.setOnItemClickListener((adapter, view, position) -> {
            messageTitleAdapter.setPos(position);
            MessageTypeListBean.DataBean bean = (MessageTypeListBean.DataBean) adapter.getItem(position);
            if (bean != null) {
                viewModel.getRecodeList(bean.getTypeValue());
            }
        });

        testRecordAdapter = new TestRecordAdapter(R.layout.test_record_item);
        binding.setLayoutManager1(new LinearLayoutManager(this));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_transparent));
        binding.tvTestRecord.addItemDecoration(divider);
        binding.setAdapter1(testRecordAdapter);
        testRecordAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            UserRecordsBean.RowsBean bean = testRecordAdapter.getItem(position);
            if (bean != null) {
                HealthQaResultActivity.start(this, bean.getQuizId(), bean.getQuizType(), bean.getId());
            }
        });

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.typeListData.observe(this, messageTitleBeans -> {
            titleBeanList = messageTitleBeans;
            messageTitleAdapter.setNewData(titleBeanList);
        });

        viewModel.recodeListData.observe(this, list -> testRecordAdapter.setNewData(list));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        }
    }
}